<?php

/**
 * Created by PhpStorm.
 * User: ls721
 * Date: 2016/10/27 0027
 * Time: 17:15
 */
class Model_Dispatch extends PhalApi_Model_NotORM
{


    public function isExist($event_id, $doctor_id)
    {
        return $this->getORM()
            ->select('*')
            ->where(array("Event_id" => $event_id, "Doctor_id" => $doctor_id))
            ->count();
    }

    public function listDispatch($doctor_id, $event_id)
    {
        $dispatch = $this->getORM();
        $dispatch->select(
            'Dispatch.id as DispatchId',
            'Event_id as EventId',
            'Doctor_id as DoctorId',
            'Event.Location as Location',
            'Event.Title as Title',
            'Dispatch.Status',
            'Dispatch.CreateDate');
        if (!empty($doctor_id)) {
            $dispatch->where('Doctor_id', $doctor_id);
        }
        if (!empty($event_id)) {
            $dispatch->where('Event_id', $event_id);
        }

        return $dispatch->fetchRows();
    }

    public function getDispatch($dispatch_id)
    {
        return $this->getORM()->select(
            'Dispatch.id as DispatchId',
            'Event_id as EventId',
            'Doctor_id as DoctorId',
            'Doctor.Name as DoctorName',
            'Dispatch.Status',
            'Dispatch.CreateDate',
            'Dispatch.ResponseTime'
        )
            ->where('Dispatch.id', $dispatch_id)
            ->fetch();
    }


    public function listDoctorOfDispatch($event_id)
    {
        return $this->getORM()
            ->select(
                'Dispatch.Doctor_id as DoctorId ',
                'Doctor.Name',
                'Doctor.DepartName',
                'Doctor.Depart',
                'Doctor.Hospital',
                'Doctor.PhoneNumber',
                'Doctor.Lng',
                'Doctor.Lat',
                'Dispatch.Status',
                'Dispatch.CreateDate',
                'Dispatch.ResponseTime'
            )
            ->where('Event_id', $event_id)
            ->order('Dispatch.CreateDate DESC')
            ->fetchRows();
    }


    protected function getTableName($id)
    {
        return 'Dispatch';
    }
}